package com.itheima.demo05stream;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class Demo06 {

    @Test //Stream流中的结果到集合中
    public void testCollect() {
        Stream<String> stream = Stream.of("aa", "bb", "cc");
        ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));
        HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));
        List<String> list = stream.collect(Collectors.toList());
        Set<String> set = stream.collect(Collectors.toSet());
    }

    @Test //Stream流中的结果到集合中
    public void testArray() {
        Stream<String> stream = Stream.of("aa", "bb", "cc");
        String[] str = stream.toArray(String[]::new);
    }

    @Test // 聚合计算
    public void testReduce() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));

        List<Student> studentList = studentStream.collect(Collectors.toList());
        //获取最大值
        Optional<Student> max = studentList.stream().max(Comparator.comparing(Student::getSocre));
        System.out.println(max.get());

        //获取最小值
        Optional<Student> min = studentList.stream().min(Comparator.comparing(Student::getSocre));
        System.out.println(min.get());

        //求总和
        int sum = studentList.stream().mapToInt(Student::getAge).sum();
        System.out.println(sum);

        //求平均值
        OptionalDouble average = studentList.stream().mapToInt(Student::getAge).average();
        System.out.println(average.getAsDouble());

        // 统计数量
        Long count = studentList.stream().count();
        System.out.println("统计数量: " + count);
    }


    @Test //对流中数据进行分组
    public void testGroup() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));

        //   Map<Integer, List<Student>> map = studentStream.collect(Collectors.groupingBy(Student::getAge));
//        Map<String,List<Student>> map = studentStream.collect(Collectors.groupingBy(s -> {
//            if (s.getSocre() > 60) {
//                return "及格";
//            } else {
//                return "不及格";
//            }
//        }));
        Map<Integer, Map<String, List<Student>>> map =
                studentStream.collect(Collectors.groupingBy(Student::getAge, Collectors.groupingBy(s -> {
                    if (s.getSocre() >= 90) {
                        return "优秀";
                    } else if (s.getSocre() >= 80 && s.getSocre() < 90) {
                        return "良好";
                    } else if (s.getSocre() >= 80 && s.getSocre() < 80) {
                        return "及格";
                    } else {
                        return "不及格";
                    }
                })));
    }

    // 分区
    @Test
    public void testPartition() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));

        Map<Boolean, List<Student>> map = studentStream.collect(Collectors.partitioningBy(s -> {
            return s.getSocre() > 90;
        }));

        map.forEach((k , v) -> {
            System.out.println(k + " :: " + v);
        });
    }


    @Test
    public void testJoining() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));

        String studentStr = studentStream.map(Student::getName).collect(Collectors.joining(","));
        System.out.println(studentStr);

    }
}
